package gameObjects;

import java.nio.FloatBuffer;

import arena.ArenaRPS;
import com.jme.image.Texture;
import com.jme.math.Plane;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.renderer.pass.RenderPass;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.water.WaterRenderPass;

/**
 * Gate per il completamento del livello
 * @author Massimiliano Palermo
 */
public class Water extends Node {
	private static final long serialVersionUID = 2904100159L;
	private WaterRenderPass waterEffectRenderPass;
    private Skybox 			skybox;
    private DisplaySystem 	display = DisplaySystem.getDisplaySystem();
    
    public void cleanup() 
    {
        waterEffectRenderPass.cleanup();
    }

    public void init(Box waterBox,Node scene)
    {
        Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();
    	buildSkyBox();
        Node reflectedNode = new Node("reflectNode");
        reflectedNode.attachChild(skybox);
        skybox.setLocalTranslation(new Vector3f(-10000, -10000, -10000));
        this.attachChild(reflectedNode);

        waterEffectRenderPass = new WaterRenderPass(cam, 4, false, true);
        waterEffectRenderPass.setWaterPlane(new Plane(new Vector3f(0.0f, 1.0f, 0.0f), 0.0f));
        
        FloatBuffer normBuf = waterBox.getNormalBuffer();
        normBuf.clear();
        normBuf.put(0).put(1).put(0);
        normBuf.put(0).put(1).put(0);
        normBuf.put(0).put(1).put(0);
        normBuf.put(0).put(1).put(0);

        waterEffectRenderPass.setWaterEffectOnSpatial(waterBox);

        waterEffectRenderPass.setReflectedScene(skybox);
        waterEffectRenderPass.setSkybox(skybox);
        ArenaRPS.getInstance().pManager.add(waterEffectRenderPass);
        
        waterEffectRenderPass.setWaterColorStart(ColorRGBA.black.clone());
        waterEffectRenderPass.setWaterColorEnd(ColorRGBA.red.clone());
        waterEffectRenderPass.setSpeedReflection(0.05f);
        waterEffectRenderPass.setSpeedRefraction(0.05f);
        waterEffectRenderPass.setWaterMaxAmplitude(0.5f);
        
        scene.attachChild(this);
        
        RenderPass rPass = new RenderPass();
        rPass.add(this);
        ArenaRPS.getInstance().pManager.add(rPass);

        this.setCullHint(Spatial.CullHint.Never);
        this.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        this.updateRenderState();
        
        waterBox.setLocalTranslation(66, 0, 66);
    }

    private void buildSkyBox() {
        skybox = new Skybox("skybox", 100f, 100f, 100f);
        skybox.setCullHint(CullHint.Never);
        String dir = "textures/water/";
        Texture tex = TextureManager.loadTexture(Water.class
                .getClassLoader().getResource(dir + "digital.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);

        skybox.setTexture(Skybox.Face.North, 	tex);
        skybox.setTexture(Skybox.Face.West, 	tex);
        skybox.setTexture(Skybox.Face.South, 	tex);
        skybox.setTexture(Skybox.Face.East, 	tex);
        skybox.setTexture(Skybox.Face.Up, 		tex);
        skybox.setTexture(Skybox.Face.Down, 	tex);
        skybox.preloadTextures();

        CullState cullState = display.getRenderer().createCullState();
        cullState.setCullFace(CullState.Face.None);
        cullState.setEnabled(true);
        skybox.setRenderState(cullState);

        ZBufferState zState = display.getRenderer().createZBufferState();
        zState.setEnabled(false);
        skybox.setRenderState(zState);

        FogState fs = display.getRenderer().createFogState();
        fs.setEnabled(false);
        skybox.setRenderState(fs);

        skybox.setLightCombineMode(Spatial.LightCombineMode.Off);
        skybox.setCullHint(Spatial.CullHint.Never);
        skybox.setTextureCombineMode(TextureCombineMode.Replace);
        skybox.updateRenderState();

        skybox.lockBounds();
        skybox.lockMeshes();
    }
}